<?php
/*
 * Application_Form_Admin class
 * Class for creating input forms to insert admin data
 * Author Nugroho Harimukti
 * Company PT Bitzero Informatika
 */
class Application_Form_Admin extends Zend_Form {

    public function init() {
        $this->setName("admin");

        //Hidden text field to pass id value
        //ID must be integer
        $admin_id = new Zend_Form_Element_Hidden("admin_id");
        $admin_id->addFilter("Int");
        
        //Admin's full name
        $name = new Zend_Form_Element_Text("name");
        $name->setLabel("Nama Lengkap")
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", "30")
                ->addErrorMessages(array("NotEmpty" => "Mohon isi nama lengkap"));

        //Admin's username/account name
        $username = new Zend_Form_Element_Text("username");
        $username->setLabel("Nama Akun")
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", "20")
                ->addErrorMessages(array("NotEmpty" => "Mohon isi nama akun"));

        //Admin's login password
        $password = new Zend_Form_Element_Password("password");
        $password->setLabel("Password (maks. 16)")
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", "16")
                ->addErrorMessages(array("NotEmpty" => "Mohon isi password"));
        $newpassword = new Zend_Form_Element_Password("newpassword");
        $newpassword->setLabel("Password Baru")
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", "16")
                ->addErrorMessages(array("NotEmpty" => "Mohon isi password"));
        $repassword = new Zend_Form_Element_Password("repassword");
        $repassword->setLabel("Tulis Ulang Password Baru ")
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", "16")
                ->addErrorMessages(array("NotEmpty" => "Mohon isi password"));
        //Admin's email
        $email = new Zend_Form_Element_Text("email");
        $email->setLabel("Email")
             ->setRequired("true")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->addValidators(array('EmailAddress'=>'EmailAddress'))
             ->addErrorMessages(array('EmailAddress'=>'Format email anda salah'))
             ->setAttrib("maxLength","100");
        
        //Admin's phone number
        $phone = new Zend_Form_Element_Text("phone");
        $phone->setLabel("Nomor Telp.")
             ->setRequired("true")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->addValidator("regex",false,array('pattern' => "/^0([0-9]{1,3}-)?[0-9]{6,}$/"))
             ->setAttrib("maxLength","15")
             ->addErrorMessages(array("regex"=>"Mohon isilah format nomor telepon dengan benar"));
        $newphone = new Zend_Form_Element_Text("newphone");
        $newphone->setLabel("No Telephone Baru")
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", "16")
                ->addErrorMessages(array("NotEmpty" => "Mohon isi nomor Telephone"));
        $rephone = new Zend_Form_Element_Text("rephone");
        $rephone->setLabel("Masukan No telephone baru")
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", "16")
                ->addErrorMessages(array("NotEmpty" => "Mohon masukan No Telephone"));
        
        //Admin's details
        //This field is not compulsory
        $details = new Zend_Form_Element_TextArea("details");
        $details->setLabel("Keterangan")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->setAttrib("maxLength","50")
             ->setAttrib("cols","20")
             ->setAttrib("rows","3");
        
        //Admin's role
        
        //First, retrive all existing roles from DB except superadmin
        $roles_db = new Application_Model_DbTable_Role();
        $roles = $roles_db->fetchAll("role_id > 1");
        $rolesArr = array();
        foreach($roles as $role){
            $rolesArr[$role->role_id] = $role->name;
        }
        
        //Next, insert roles into select element
        $select_role = new Zend_Form_Element_Select("role");
        $select_role->setRequired(true)
                 ->setLabel("Peran")
                 ->addMultiOptions($rolesArr);

        //Submit button
        $submit = new Zend_Form_Element_Submit("submit");
        $submit->setAttrib("id","submit");

        //Reset button
        $reset = new Zend_Form_Element_Reset("reset");
        $reset->setAttrib("id","reset");

        //Add all elements to form
        $this->addElements(array($admin_id,$name,$username,$password,$email,
                                $phone,$details,$select_role,$submit,$reset,
                                $newpassword,$repassword,$newphone,$rephone));
    }

}
